package com.baidu.ota.utils;

import android.text.TextUtils;
import android.util.Log;

import com.baidu.ota.meta.Config;
import com.baidu.otasdk.ota.Constants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class StringUtil {

    private StringUtil() {
    }

    public static List<Config.Pair<Integer, String>> parse2IntegerList(String org) {
        List<Config.Pair<Integer, String>> list = new ArrayList<>();
        if (TextUtils.isEmpty(org)) {
            return list;
        }
        String[] splits = org.split(",");
        for (String split : splits) {
            String[] ss = split.split(":");
            if (ss.length != 2) {
                Log.e(Constants.TAG, "config length:" + org);
                continue;
            }

            try {
                Config.Pair<Integer, String> config = new Config.Pair<>();
                config.key = Integer.parseInt(ss[0]);
                config.val = ss[1];

                list.add(config);
            } catch (Exception e) {
                Log.w(Constants.TAG, "parse option:" + e.toString());
            }
        }
        return list;
    }

    public static List<String> parse2List(String org) {
        if (TextUtils.isEmpty(org)) {
            return null;
        }
        String[] splits = org.split(",");
        return Arrays.asList(splits);
    }
}
